﻿/*
 * Created by SharpDevelop.
 * User: user
 * Date: 11/09/2011
 * Time: 20:15
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using csharp.rad.lavanderia.classes;

namespace csharp.rad.lavanderia.entries
{
	/// <summary>
	/// Description of frmRegisterService.
	/// </summary>
	public partial class frmRegisterService : KryptonForm
	{
		Service service = new Service();
		ServiceDetail serviceDetail = new ServiceDetail();
		ServiceDetails listServiceDetail = new ServiceDetails();
		
		public frmRegisterService(string service_id)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			
			service.id = service_id;
		}
		
		void FrmRegisterServiceLoad(object sender, EventArgs e)
		{			
			fillComboCustomer();
			
			clear();
			if( !service.id.Equals(String.Empty))
			{
				if(!service.get())
				{
					KryptonMessageBox.Show("Erro ao buscar registro:\n"+service.errorMessage);
					return;
				}
				listServiceDetail.fillListByService(service.id);
				setEdits();
				fillDataGridSeviceDetail();
			}
			else
				generateServiceNumber();
		}
		
		void BtnSaveDetailClick(object sender, EventArgs e)
		{
			saveServiceDetail();
			txtservice_quant.Text= "0.00";
			txtservice_description.Text= String.Empty;
			txtservice_process.Text= String.Empty;
			txtservice_price.Text= "0.00";
		}
		
		void fillDataGridSeviceDetail()
		{
			dgvServiceDetail.Rows.Clear();
			
			for(int i=0; i < listServiceDetail.count(); i++)
			{
				String[] row = new String[]{listServiceDetail.get(i).id,
					listServiceDetail.get(i).quant,
					listServiceDetail.get(i).description,
					listServiceDetail.get(i).process,
					listServiceDetail.get(i).price,
					mult(listServiceDetail.get(i).quant, listServiceDetail.get(i).price)};
				dgvServiceDetail.Rows.Add(row);
			}
			calcTotal();
			calcTotaltoPay();
		}
		
		void fillComboCustomer()
		{
			classes.Customers listCustomers = new csharp.rad.lavanderia.classes.Customers();			
			
			listCustomers.fillList();
			
			cmbCustomers.Items.Clear();
			for(int i=0; i < listCustomers.count(); i++)
			{
				cmbCustomers.Items.Add(listCustomers.get(i));
			}
			cmbCustomers.DisplayMember = "name";
			cmbCustomers.ValueMember = "id";			
		}
		
		void clear()
		{
			cmbCustomers.SelectedIndex= -1;
			txtservice_situation.SelectedIndex= 0;
			dtpDate.Text= DateTime.Now.ToShortDateString();
			txtNumber.Text= string.Empty;
			
			txtservice_total.Text= "0.00";
			txtservice_total_pay.Text= "0.00";
			txtTotalRest.Text= "0.00";			
			
			txtservice_quant.Text= "0.00";
			txtservice_description.Text= String.Empty;
			txtservice_process.Text= String.Empty;
			txtservice_price.Text= "0.00";			
		}
		
		void saveService(bool showMessage)
		{
			String msg= String.Empty;
			
			setClassService();
			
			if( service.id.Equals(String.Empty))
			{
				if( service.save() )
					msg= "Serviço salvo com sucesso!";
				else
					msg= "Erro ao salvar serviço:\n"+service.errorMessage;
			}
			else
			{
				if( service.update() )
					msg= "Serviço salvo com sucesso!";
				else
					msg= "Erro ao salvar serviço:\n"+service.errorMessage;
			}
			if(showMessage)
				KryptonMessageBox.Show(msg);
		}
		
		void saveServiceDetail()
		{
			if( service.id.Equals(String.Empty))
				saveService(true);
			
			setClassServiceDetail();
			
			if( serviceDetail.id.Equals(String.Empty))
			{
				if(serviceDetail.save())
				{
					listServiceDetail.fillListByService(service.id);
					fillDataGridSeviceDetail();
				}
				else
				{
					KryptonMessageBox.Show("Erro ao salvar registro:\n"+serviceDetail.errorMessage);
				}				
			}
			else
			{
				if(serviceDetail.update())
				{
					listServiceDetail.fillListByService(service.id);
					fillDataGridSeviceDetail();
				}
				else
				{
					KryptonMessageBox.Show("Erro ao salvar registro:\n"+serviceDetail.errorMessage);
				}	
			}
			
			serviceDetail.clear();
			calcTotal();
			//Salva os campos recalculados
			saveService(false);
		}
		
		void setClassService()
		{
			service.customer_id = (cmbCustomers.SelectedItem as Customer).id;
			service.date = dtpDate.Text;
			service.number = txtNumber.Text;
			service.total = txtservice_total.Text;
			service.total_pay = txtservice_total_pay.Text;
			
			//ABERTO
			if(txtservice_situation.SelectedIndex.Equals(0))
			{
				service.situation = "A";
			}
			else if( txtservice_situation.SelectedIndex.Equals(1)) //PARCIALMENTO PAGO
			{
				service.situation= "M";
			}
			else if( txtservice_situation.SelectedIndex.Equals(2)) //PAGO
			{
				service.situation = "P";
			}
		}
		
		void setClassServiceDetail()
		{
			serviceDetail.quant= txtservice_quant.Text;
			serviceDetail.description= txtservice_description.Text;
			serviceDetail.process = txtservice_process.Text;
			serviceDetail.price = txtservice_price.Text;
			serviceDetail.total= mult(txtservice_quant.Text, txtservice_price.Text);
			serviceDetail.service_id = service.id;
		}
		
		void setEdits()
		{
			int tam = cmbCustomers.Items.Count;
			
			for(int i=0; i < tam; i++)
			{
				if( (cmbCustomers.Items[i] as Customer).id.Equals(service.customer_id))
				{
					cmbCustomers.SelectedIndex= i;
					break;
				}
			}
			dtpDate.Value= Convert.ToDateTime(service.date);
			txtNumber.Text= service.number;
			txtservice_total.Text= common.Util.formatDoubleToString(service.total);
			txtservice_total_pay.Text= common.Util.formatDoubleToString(service.total_pay);
			if(service.situation.Equals("A"))
			{
				txtservice_situation.SelectedIndex= 0;
			}
			else if( service.situation.Equals("M")) //PARCIALMENTO PAGO
			{
				txtservice_situation.SelectedIndex= 1;
			}
			else if( service.situation.Equals("P")) //PAGO
			{
				txtservice_situation.SelectedIndex= 2;
			}			
			//calcTotalToPay();
		}
		
		void setEditsServiceDetails()
		{
			txtservice_quant.Text= serviceDetail.quant;
			txtservice_description.Text= serviceDetail.description;
			txtservice_process.Text= serviceDetail.process;
			txtservice_price.Text= common.Util.formatDoubleToString(serviceDetail.price);
		}
		
		string mult(string a, string b)
		{
			Double x, y;
		
			x= Convert.ToDouble(a);
			y= Convert.ToDouble(b);
			
			return Convert.ToString(x*y);
		}
		
		void calcTotal()
		{
			int tam = dgvServiceDetail.Rows.Count;
			double total, aux;
			
			total = aux= 0;
			for(int i=0; i < tam; i++)
			{
				aux= common.Util.formatDouble(dgvServiceDetail.Rows[i].Cells["service_detail_total"].Value.ToString());
				total+= aux;
			}

			txtservice_total.Text = total.ToString("0.00");
		}
		
		void calcTotaltoPay()
		{
			double a, b, total;
			
			a= common.Util.formatDouble(txtservice_total.Text);
			b= common.Util.formatDouble(txtservice_total_pay.Text);
			
			total= a - b;	
			txtTotalRest.Text= total.ToString("0.00");
		}
		
		void Txtservice_total_payTextChanged(object sender, EventArgs e)
		{
			//calcTotaltoPay();
		}
		
		void BtnSaveClick(object sender, EventArgs e)
		{
			saveService(true);
		}
		
		void Txtservice_total_payLeave(object sender, EventArgs e)
		{
			calcTotaltoPay();
		}
		
		void BtnNovoClick(object sender, EventArgs e)
		{
			service.clear();
			
			listServiceDetail.clear();
			
			dgvServiceDetail.Rows.Clear();
			
			clear();
			generateServiceNumber();
		}
		
		void generateServiceNumber()
		{
			int aux= new classes.Services().getMaxNumber();
			
			txtNumber.Text= (aux+1).ToString();
		}
		
		void BtnExitClick(object sender, EventArgs e)
		{
			this.Close();
		}
		
		void DgvServiceDetailDoubleClick(object sender, EventArgs e)
		{
			if(dgvServiceDetail.SelectedRows.Count <=0)
			{
				KryptonMessageBox.Show("Selecione um registro!", ProductName);
				return;
			}
			serviceDetail.id= dgvServiceDetail.SelectedRows[0].Cells["service_detail_id"].Value.ToString();
			serviceDetail.get();
			setEditsServiceDetails();
		}
		
		void BtnAddCustomerClick(object sender, EventArgs e)
		{
			entries.frmRegisterCustomer form = new frmRegisterCustomer();
			form.ShowDialog();
			
			fillComboCustomer();
		}
		
		void Txtservice_quantLeave(object sender, EventArgs e)
		{
			
		}
	}
}
